/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ipostservices.services;

import ipostservices.model.bean.Project;
import ipostservices.model.bean.User;
import ipostservices.model.dao.ProjectDAO;
import ipostservices.model.dao.UserDAO;
import ipostservices.util.Constantes;
import java.util.ArrayList;

/**
 *
 * @author sixviv
 */
public class UserSrv {
    UserDAO uDAO = new UserDAO();
    ProjectDAO pDAO = new ProjectDAO();

    public String guardar(User user){
        if(uDAO.insertar(user)){
            return Constantes.EXITO_STR;
        }
        else{
            return Constantes.ERROR_STR;
        }
    }

    public String actualizar(User user){
        if(uDAO.actualizar(user)){
            return Constantes.EXITO_STR;
        }
        else{
            return Constantes.ERROR_STR;
        }
    }

    public User validacion(String email, String password){
        User user = uDAO.getByEmail(email);
        if(user!=null){
            if(user.getPassword().compareTo(password)==0){
                return user;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    public String eliminar(int idUser){
        if(uDAO.borrar(uDAO.getById(idUser))){
            return Constantes.EXITO_STR;
        }
        else{
            return Constantes.ERROR_STR;
        }
    }

    public User getUser (int idUser){
        return uDAO.getById(idUser);
    }

    public String abandonarProy(int idProy, int idUser){
        User user = getUser(idUser);
        ArrayList<Project> projects = new ArrayList<Project>();
        projects.addAll(user.getProjects_1());
        for(Project project : projects){
            if(project.getIdproject()==idProy){
                user.getProjects_1().remove(project);
            }
        }
        return this.actualizar(user);
    }

}
